package DBLayer;
import ModelLayer.*;
import java.sql.*;


public class DBFaktura implements IFDBFaktura {
	private Connection con;
	
	public DBFaktura() {
		con = DBConnection.getInstance().getDBcon();
	}
	
	public Faktura findFaktura(int fakturaID, boolean retriveAssociation)
    {   String wClause = "  fakturaid = '" + fakturaID + "'";
        return singleWhere(wClause, retriveAssociation);
    }
	
	private Faktura singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Faktura fObj = new Faktura();
                
	        String query =  buildQuery(wClause);
                System.out.println(query);
		try{ // read the employee from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
                            fObj = buildFaktura(results);
                            //assocaition is to be build
                            stmt.close();
                            
			}
                        else{ //no employee was found
                            fObj = null;
                        }
		}//end try	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return fObj;
	}
	
	private String buildQuery(String wClause)
	{
	    String query="SELECT *  FROM faktura";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	
	private Faktura buildFaktura(ResultSet results)
    {   Faktura fObj = new Faktura();
        try{ // the columns from the table emplayee  are used
              fObj.setFakturaID(results.getInt("fakturaid"));
              fObj.setDato(results.getString("dato"));
              }
       catch(Exception e)
       {
           System.out.println("error in building the employee object");
       }
       return fObj;
    }
	
}
